home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 November: Tool Chest / Dev.CD Nov 98 TC.toast / Sample Code / Snippets / Toolbox / ListInDialog / Source / ListInDialog.c
Encoding:
C/C++ Source or Header  |  1996-09-17  |  6.6 KB  |  244 lines  |  [TEXT/CWIE]

  1. /*
  2. ListInDialog
  3.  
  4. A snippet that shows how to (uuuhhhhh) put a list in a dialog.
  5.  
  6. I thought we had this one, but I guess not. It's easy, just create the list
  7. right after you create the dialog, then call LUpdate and LClick in a 
  8. dialog filter to respond to user events.
  9.  
  10. Please see the snippet DialogBits for a more comprehensive treatment of
  11. everything you can do in a dialog (well, almost).
  12.  
  13. This is a 680x0 version, built with the Universal Headers.
  14.  
  15. C.K. Haun
  16. April '94
  17. Are you sure we don't already have a snippet like this????
  18. Oh, I rememeber, that was on the //gs
  19.  
  20. */
  21.  
  22. #include <Dialogs.h>
  23. #include <Controls.h>
  24. #include <QuickDraw.h>
  25. #include <Windows.h>
  26. #include <ToolUtils.h>
  27. #include <OSUtils.h>
  28. #include <Menus.h>
  29. #include <Fonts.h>
  30. #include <resources.h>
  31. #include <Sound.h>
  32. #include <Traps.h>
  33. #include <Gestaltequ.h>
  34. #include <Memory.h>
  35. #include <Scrap.h>
  36. #include <TextEdit.h> 
  37. #include <Lists.h>
  38. #include <Events.h>
  39. #include <string.h>
  40.  
  41. enum  {
  42.     kSampleDialog = 128
  43. };
  44. enum  {
  45.     kListItem = 3
  46. };
  47.  
  48. pascal Boolean theListFilter(DialogPtr theDialog, EventRecord *theEvent, short *itemHit);
  49.  
  50. void SetUpList(ListHandle theList);
  51.  
  52. /* making the list handle a global */
  53. ListHandle gAList;
  54.  
  55. void main(void)
  56. {
  57.     
  58.     // this "rect" defines the data bounds of the list. In this case, a
  59.     // one column list
  60.     Rect listRect2 =  {
  61.         0, 0, 0, 1
  62.     };
  63.     // cell to initialize with
  64.     Cell cp =  {
  65.         0, 0
  66.     };
  67.     
  68.     // the dialog we're using
  69.     DialogPtr myDialog = nil;
  70.     
  71.     // hitItem for ModalDialog call
  72.     short hitItem = 0;
  73.     
  74.     // these three are here for all the GetDItem/SetDItem calls
  75.     Rect tempRect;
  76.     short tempItem;
  77.     Handle tempHandle;
  78.     
  79.     static ModalFilterUPP modalFilterUPP;
  80.     
  81.     modalFilterUPP = NewModalFilterProc(theListFilter);
  82.    
  83.     /* start up managers */
  84.     InitGraf((Ptr)&qd.thePort);
  85.     InitFonts();
  86.     InitWindows();
  87.     InitMenus();
  88.     TEInit();
  89.     InitDialogs(nil);
  90.     InitCursor();
  91.    
  92.     
  93.     /* get our dialog. It is created HIDDEN, and shown after I set the */
  94.     /* user item that will hold the list */
  95.  
  96.     myDialog = GetNewDialog(kSampleDialog, nil, (WindowPtr)-1);
  97.     
  98.     // setting it up in the Dialog manager's records, 
  99.     GetDialogItem(myDialog, kListItem, &tempItem, &tempHandle, &tempRect);
  100.     
  101.     /* inset the rect by 16, which is the width of the scroll bar that will be attached */
  102.     /* to this list */
  103.     tempRect.right -= 16;
  104.     
  105.     /* set the current port to this dialog */
  106.     SetPort(myDialog);
  107.     
  108.     /* create the list */
  109.     
  110.     gAList = LNew(&tempRect,    // in the tempRect bounds
  111.         &listRect2,                // with a sinngle column
  112.         cp,                        // default cell size (a cell of 0,0 says that)
  113.         nil,                    // no special LDEF, use the standard one
  114.         myDialog,                // put it in this port
  115.         false,                    // do NOT draw initially
  116.         false,                    // does NOT have a grow box space
  117.         false,                    // does NOT scroll horizontally
  118.         true);                    // DOES have a verticle scroll bar
  119.     
  120.     // call another funtion to put in the strings
  121.     SetUpList(gAList);
  122.     
  123.     // turn drawing on after the list has been filled
  124.     LSetDrawingMode(true, gAList);
  125.     
  126.     // show the dialog
  127.     ShowWindow((WindowPtr)myDialog);
  128.     
  129.     /* draw it once */
  130.     DrawDialog(myDialog);
  131.     
  132.     // loop until ModalDialog is done
  133.     do {
  134.         // we have to use a ModalDialog filter, since ModalDialog doesn't
  135.         // automatically handle lists
  136.         ModalDialog(modalFilterUPP, &hitItem);
  137.         
  138.         // switch off what item was hit
  139.         switch (hitItem) {
  140.             /* I don't care about any of the hits here */
  141.             
  142.         }
  143.         // wait for an OK or Cancel
  144.     }
  145.             while (hitItem != ok && hitItem != cancel);
  146.     
  147.     // don't do anything in this sample
  148.     DisposeDialog(myDialog);
  149. }
  150.  
  151.  
  152. /* Here is the filter that handles any List activity */
  153. pascal Boolean theListFilter(DialogPtr theDialog, EventRecord *theEvent, short *itemHit)
  154. {
  155.     
  156.     Rect tempR;
  157.     Boolean returnValue = false;    // defaults to me not saying I handled anything
  158.     Boolean theBoolean;
  159.     WindowPtr oldP;
  160.  
  161.     // get the current port, set the port to this dialog
  162.     GetPort(&oldP);
  163.     SetPort(theDialog);
  164.     
  165.     // was this an update event for this dialog?
  166.     if ((theEvent->what == updateEvt) && (theEvent->message == (UInt32)theDialog)) {
  167.         
  168.         
  169.         // Update the list.
  170.         LUpdate(theDialog->visRgn, gAList);
  171.         
  172.         // get the list rectangle
  173.         tempR = (*gAList)->rView;
  174.         
  175.         // push it outwards one pixel and frame the list
  176.         InsetRect(&tempR, -1, -1);
  177.         FrameRect(&tempR);
  178.         
  179.         // NOTE: Do !NOT! return 'true' if you did SOME drawing in response to a dialog update
  180.         // event in your filter!
  181.         // ONLY if you did EVERYTHING should you return 'true', or else you can cause other updating
  182.         // not to occur, which would be Bad
  183.         
  184.     } else {
  185.         // see if this was a mouseDown event
  186.         if (theEvent->what == mouseDown) {
  187.             Point theP;
  188.  
  189.             // we set the port to the dialog on entry to the filter, so a GetMouse will work
  190.             GetMouse(&theP);
  191.             
  192.             // get the list rectangle
  193.             tempR = (*gAList)->rView;
  194.             
  195.             // add the scroll bar back in for hit testing (remember we took it out earlier)
  196.             tempR.right += 16;
  197.             
  198.             // See if they clicked in our list!
  199.             if (PtInRect(theP, &tempR)) {
  200.                 theBoolean = LClick(theP, nil, gAList);
  201.                 // if they double-clicked the list, return 1, as if the OK button had been pressed
  202.                 if (theBoolean)
  203.                     *itemHit = 1;
  204.                 else
  205.                     *itemHit = kListItem;
  206.                 
  207.                 // tell the Dialog Manager that we handled this click, it can stop searching for a click-owner
  208.                 returnValue = true;
  209.             }
  210.         }
  211.     }
  212.     
  213.     // reset the original port
  214.     
  215.     SetPort(oldP);
  216.     
  217.     return(returnValue);
  218. }
  219.  
  220.  
  221. /* simple function to put a list of strings into my list */
  222. void SetUpList(ListHandle theList)
  223. {
  224.     register qq;
  225.     Cell cp;
  226.     Str255 theString;
  227.     short listCount = 0;
  228.     Boolean Changed = false;
  229.     short theCount;
  230.     Handle tempHandle = GetResource('STR#', 128);
  231.     if(tempHandle){
  232.     theCount = *((short *)(*tempHandle));
  233.         if (theCount) {
  234.             for (qq = 0; qq < theCount; qq++) {
  235.                 GetIndString(theString, 128, qq + 1);
  236.                 listCount = LAddRow(1, listCount + 1, theList);
  237.                 cp.h = 0;
  238.                 cp.v = listCount;
  239.                 LAddToCell(&theString[1], theString[0], cp, theList);
  240.             }
  241.         }
  242.     }
  243. }
  244.